package com.simple.action.fee;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.EmpEmployee;
import com.simple.po.FeeOtherFee;
import com.xunj.action.common.AbstractAction;
import com.xunj.core.ParaUnit;
import com.xunj.core.SessionBean;
import com.xunj.util.Common;

/**
 * 奖惩登记
 * @author 王瑾
 *
 */
public class RewardsPunishmentAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2755331135579273365L;
	/**
	 * 
	 */
	private FeeOtherFee feeOtherFee; //其他费用信息，奖惩信息


	
	/**
	 * 查询奖惩信息
	 * @return
	 * @throws Exception
	 */
	public String listRewardsPunishment() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
		
		paralist.add(new ParaUnit("otherFeeType", "奖惩", ParaUnit.EQ));
		paralist.add(new ParaUnit("state", "在用", ParaUnit.EQ));
		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		
		if(POST())
		{
			if(!feeOtherFee.getReportState().equals(""))
				paralist.add(new ParaUnit("reportState",feeOtherFee.getReportState(), ParaUnit.EQ));
			if(!feeOtherFee.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId",feeOtherFee.getAreaId(), ParaUnit.EQ));
			
			String dateFrom = request.getParameter("dateFrom");
			String dateTo = request.getParameter("dateTo");
			if(!dateFrom.equals(""))
			{
				paralist.add(new ParaUnit("otherFeeDate",Common.newDate(dateFrom+" 00:00:00", "yyyy-MM-dd"), ParaUnit.GE));
				request.setAttribute("dateFrom", dateFrom);
			}
			if(!dateTo.equals(""))
			{
				paralist.add(new ParaUnit("otherFeeDate",Common.newDate(dateTo+" 23:59:59", "yyyy-MM-dd"), ParaUnit.LE));
				request.setAttribute("dateTo", dateTo);
			}
			if(!feeOtherFee.getEmployeeName().equals(""))
				paralist.add(new ParaUnit("employeeName", feeOtherFee.getEmployeeName(), ParaUnit.LIKE));
			
			String otherFeeClassesLvl = request.getParameter("otherFeeClassesLvl");
			
			if(feeOtherFee.getOtherFeeClasses()!=null&&!feeOtherFee.getOtherFeeClasses().equals(""))
				paralist.add(new ParaUnit("otherFeeClasses", feeOtherFee.getOtherFeeClasses(), ParaUnit.EQ));
			else
			{
				if(!otherFeeClassesLvl.equals(""))
					paralist.add(new ParaUnit("otherFeeClasses", otherFeeClassesLvl, ParaUnit.LEFT_LIKE));
			}
			if(!feeOtherFee.getOtherFeeYear().equals(""))
				paralist.add(new ParaUnit("otherFeeYear", feeOtherFee.getOtherFeeYear(), ParaUnit.EQ));
			if(!feeOtherFee.getOtherFeeMonth().equals(""))
				paralist.add(new ParaUnit("otherFeeMonth", feeOtherFee.getOtherFeeMonth(), ParaUnit.EQ));
		}
		List list = dao.criteriaByPage(FeeOtherFee.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/rewards_punishment/lst_rewards_punishment.jsp");
	}
	
	/**
	 * 添加奖惩信息
	 * @return
	 * @throws Exception
	 */
	public String addRewardsPunishment() throws Exception
	{
		//提交类型，save直接保存、saveAndAdd保存后复制添加
		String submitType = request.getParameter("submitType");
		if(POST())
		{
			HttpServletRequest request = ServletActionContext.getRequest();
			SessionBean sessionBean  = SessionBean.getSessionBean(request);
			feeOtherFee.setOtherFeeId(UUID.randomUUID().toString());
			feeOtherFee.setState("在用");
			feeOtherFee.setCreateUserId(sessionBean.getUserId());
			feeOtherFee.setCreateUserName(sessionBean.getUserName());
			feeOtherFee.setReportState("未上报");
			feeOtherFee.setOtherFeeType("奖惩");
			dao.save(feeOtherFee);
			if("save".equals(submitType))
				this.Alert_Close("保存成功", "ok");
			else
			{
				ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
				
				paralist.add(new ParaUnit("state", "A", ParaUnit.EQ));
				paralist.add(new ParaUnit("areaId", ParaUnit.ASC,ParaUnit.ORDER));
				paralist.add(new ParaUnit("employeeOrder", ParaUnit.ASC,ParaUnit.ORDER));
				
				List list = dao.criteriaByPage(EmpEmployee.class, paralist);
				request.setAttribute("employeelist", list);
				return toJsp("/jsp/fee/rewards_punishment/add_rewards_punishment.jsp");
			}
			return toMessage();
		}
		else
		{
			return toJsp("/jsp/fee/rewards_punishment/add_rewards_punishment.jsp");
		}
	}
	/**
	 * 修改奖惩信息
	 * @return
	 * @throws Exception
	 */
	public String updateRewardsPunishment() throws Exception
	{
		//提交类型，save直接保存、saveAndAdd保存后复制添加
		String submitType = request.getParameter("submitType");
		if(POST())
		{
			feeOtherFee.setOtherFeeType("奖惩");
			dao.update(feeOtherFee);
			
			if("save".equals(submitType))
				this.Alert_Close("保存成功", "ok");
			else
			{
				ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
				
				paralist.add(new ParaUnit("state", "A", ParaUnit.EQ));
				paralist.add(new ParaUnit("areaId", ParaUnit.ASC,ParaUnit.ORDER));
				paralist.add(new ParaUnit("employeeOrder", ParaUnit.ASC,ParaUnit.ORDER));
				
				List list = dao.criteriaByPage(EmpEmployee.class, paralist);
				request.setAttribute("employeelist", list);
				return toJsp("/jsp/fee/rewards_punishment/add_rewards_punishment.jsp");
			}
			return toMessage();
		}
		else
		{
			String otherFeeId = request.getParameter("otherFeeId");
			feeOtherFee = (FeeOtherFee)dao.findOne(FeeOtherFee.class, otherFeeId);
			ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
			
			paralist.add(new ParaUnit("state", "A", ParaUnit.EQ));
			paralist.add(new ParaUnit("areaId", ParaUnit.ASC,ParaUnit.ORDER));
			paralist.add(new ParaUnit("employeeOrder", ParaUnit.ASC,ParaUnit.ORDER));
			
			List list = dao.criteriaByPage(EmpEmployee.class, paralist);
			request.setAttribute("employeelist", list);
			return toJsp("/jsp/fee/rewards_punishment/update_rewards_punishment.jsp");
		}
	}
	/**
	 * 删除奖惩信息
	 * @return
	 * @throws Exception
	 */
	public String deleteRewardsPunishment() throws Exception
	{
		String otherFeeId = request.getParameter("otherFeeId");
		feeOtherFee = (FeeOtherFee)dao.findOne(FeeOtherFee.class, otherFeeId);
		feeOtherFee.setState("删除");
		this.Alert_Close("删除成功", "ok");
		return toMessage();
	}
	/**
	 * 上报奖惩信息
	 * @return
	 * @throws Exception
	 */
	public String reportRewardsPunishment() throws Exception
	{
		String otherFeeIds = request.getParameter("otherFeeIds");
		

		String instr = "";
		
		for(String s:otherFeeIds.split(","))
		{
			instr+="'"+s+"',";
		}
		instr=instr.substring(0, instr.length()-1);

		String hql = "update FeeOtherFee f set " +
				"f.reportState='已上报' ,f.reportDate='"+Common.getCurrentDateTime()+"' where otherFeeId in ("+instr+")";
		dao.bulkUpdate(hql);
		
		this.Alert_GoUrl("上报成功", "listRewardsPunishment.do");
		return toMessage();
	}
	/**
	 * 查看奖惩信息
	 * @return
	 * @throws Exception
	 */
	public String showRewardsPunishment() throws Exception
	{
		
		String otherFeeId = request.getParameter("otherFeeId");
		feeOtherFee = (FeeOtherFee)dao.findOne(FeeOtherFee.class, otherFeeId);

		return toJsp("/jsp/fee/rewards_punishment/show_rewards_punishment.jsp");
	}

	public FeeOtherFee getFeeOtherFee() {
		return feeOtherFee;
	}

	public void setFeeOtherFee(FeeOtherFee feeOtherFee) {
		this.feeOtherFee = feeOtherFee;
	}





}